<template>
  <div class="bill">
    <div class="bill-top">
      <div class="link-box">
        <router-link :to="'/wallet/my?type='+type" class="fl"><i class="iconfont">&#xe6c3;</i>{{$t('btn.back')}}</router-link>
      </div>
      <p>{{$t('wallet.walletCount.bill')}}</p>
      <div class="date-box">
        <el-date-picker
          :disabled="walletLog_loading"
          v-model="time"
          :clearable="false"
          type="daterange"
          value-format="yyyy/MM/dd"
          :picker-options="pickerOptions"
          :range-separator="$t('wallet.bill.to')"
          :start-placeholder="$t('wallet.bill.start_date')"
          :end-placeholder="$t('wallet.bill.end_date')"
          @change="changeTime"
          >
        </el-date-picker>
        <p>
          <span>{{$t('wallet.bill.total_cash')}} <spinner v-if="walletInocme_loading"/><template v-else>{{walletInocme_data.out_amount | d8fixed}}</template></span>
          <span>{{$t('dashboard.total_income')}} <spinner v-if="walletInocme_loading"/><template v-else>{{walletInocme_data.in_amount | d8fixed}}</template></span>
        </p>
      </div>
    </div>
    <div class="bill-content">
      <el-table v-loading="walletLog_loading" :data="list" border style="width: 100%">
        <el-table-column :label="$t('wallet.bill.classify')">
           <template slot-scope="scope">
             <div class="type" :class="scope.row.log_type">
               <span>{{ scope.row.log_type === 'in'? $t('wallet.bill.income'):$t('wallet.bill.cash') }}</span>
            </div>
             <div class="date">{{scope.row.create_time}}</div>
            </template>
        </el-table-column>
        <el-table-column :label="$t('wallet.bill.address')">
          <template slot-scope="scope">
            <span class="tt" style="margin-left: 10px">{{ scope.row.address}}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('wallet.bill.serviceCharge')+ type.toUpperCase()">
          <template slot-scope="scope">
            <span class="tt" style="margin-left: 10px">{{ scope.row.fee | d8fixed }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('wallet.bill.money')+ type.toUpperCase()">
          <template slot-scope="scope">
            <span class="tt" style="margin-left: 10px">{{ scope.row.money | d8fixed }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination :page="page | num" :pageSize="limit | num" :totalCount="total_count | num" :disabled="walletLog_loading" :sizeChange="sizeChange" :currentChange="currentChange"/>
    </div>
  </div>
</template>
<script>
import { walletLog, walletIncome } from '@/api';
import pagination from '@/components/Pagination';
import spinner from '@/components/Spinner';
import { mapGetters } from 'vuex';
import moment from 'moment';

export default {
  components: { pagination, spinner },
  data() {
    return {
      walletLog_loading: false,
      limit: 10,
      page: 1,
      total_count: 0,
      type: '',
      time: [],
      list: [],
      walletInocme_data: {
        in_amount: 0,
        out_amount: 0,
      },
      walletInocme_loading: false,
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },
    };
  },
  computed: {
    ...mapGetters(['id']),
    start_time() {
      return this.time ? this.time[0] : '';
    },
    end_time() {
      return this.time ? this.time[1] : '';
    },
  },
  beforeCreate() {
    if (this.$route.query.type === 'buc' || this.$route.query.type === 'mut') {
      this.$router.push('/404');
    }
  },
  created() {
    const endTime = moment().format('YYYY/MM/DD');
    const arr = endTime.split('/');
    const startTime = `${arr[0]}/${arr[1]}/01`;
    this.time = [startTime, endTime];
    this.type = this.$route.query.type || 'btc';
    this.walletIncome();
    this.walletLog();
  },
  methods: {
    sizeChange(limit) {
      this.limit = limit;
      this.page = 1;
      this.walletLog();
    },
    currentChange(page) {
      this.page = page;
      this.walletLog();
    },
    changeTime() {
      this.page = 1;
      this.walletLog();
    },
    walletIncome() {
      this.walletInocme_loading = true;
      walletIncome({ parent_uid: this.id }).then((res) => {
        if (res.data.status === 1) {
          if (res.data.data[this.type]) {
            this.walletInocme_data = res.data.data[this.type];
          }
        } else {
          this.$message.error(this.$t(`api.${res.data.errcode}`));
        }
        this.walletInocme_loading = false;
      }).catch(() => {
        this.$message.error(this.$t('error.catchError'));
        this.walletInocme_loading = false;
      });
    },
    walletLog() {
      this.walletLog_loading = true;
      walletLog({
        type: this.type,
        start_time: this.start_time,
        end_time: this.end_time,
        page: this.page,
        limit: this.limit,
        parent_uid: this.id,
      }).then((res) => {
        if (res.data.status === 1) {
          this.list = res.data.data;
          this.total_count = res.data.count;
        } else {
          this.$message.error(this.$t(`api.${res.data.errcode}`));
        }
        this.walletLog_loading = false;
      }).catch((() => {
        this.$message.error(this.$t('error.catchError'));
        this.walletLog_loading = false;
      }));
    },
  },
};
</script>
<style scoped lang="scss">
.bill {
  > div {
    box-sizing: border-box;
  }
  .bill-top {
    padding: 0 30px;
    padding-bottom: 15px;
    box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.05);
    border: solid 1px #e0e0e0;
    background-color: #ffffff;
    .link-box {
      height: 33px;
      line-height: 33px;
      font-size: 12px;
      color: #999999;
      a{
        position: relative;
        color: #999999;
        i {
          position: absolute;
          top: 48%;
          transform: translateY(-50%);
          left: -18px;
          font-size: 18px;
        }
      }
    }
    p {
      text-align: left;
      font-size: 24px;
      color: #70c1b3;
      line-height: 36px;
      height: 36px;
    }
    .date-box {
      position: relative;
      margin-top: 8px;
      height: 80px;
      background-color: #f9f9f9;
      padding: 10px 30px;
      font-size: 22px;
      color: #323232;
      cursor: pointer;
      box-sizing: border-box;
      p {
        text-align: left;
        margin-left: 10px;
        margin-top: -8px;
        span {
          color: #247ba0;
          font-size: 14px;
          margin-right: 50px;
        }
      }
    }
  }
  .bill-content {
    margin-top: 20px;
    padding: 0;
    box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.05);
    border: solid 1px #e0e0e0;
    background-color: #ffffff;
    padding-bottom: 20px;
    .type{
      &.out{
        color: #247ba0;
      }
      &.in{
        color: #70c1b3;
      }
    }
    .date{
      font-size: 12px;
      color: #8a8a8a;
    }
  }
}
</style>
